/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elsimulatorjava;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

/**
 *
 * @author corey
 */
public class FloorDoorPanel extends JPanel {
    private static final short ELEVATOR_WIDTH = 100;
    private static final short ELEVATOR_HEIGHT = 200;
    private static final byte BUTTON_WIDTH = 50;
    private static final byte BORDER = 30;
    
    private boolean redraw;
    private Door[] doors;
    private ArrayList<Short> elevatorIdx;
    
    private short floorIdx;
    
    private Elevator[] elevators;
    private Floor[] floors;
    
    
    int border = 12;
    
    public FloorDoorPanel(Elevator[] e, Floor[] f){
        elevators = e;
        floors = f;
        floorIdx = 0;
        doors = new Door[elevators.length];
        elevatorIdx = new ArrayList<Short>();
        
        for (int i = 0; i < elevators.length; i++) {
                if (elevators[i].goesToFloor(floorIdx)){
                    elevatorIdx.add(elevators[i].getIndex());
                }
            }
        
        
        for (int i = 0; i < elevators.length; i++){
            doors[i] = new Door(ELEVATOR_WIDTH, ELEVATOR_HEIGHT);
        }
        this.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(floors[floorIdx].getName()), 
                    BorderFactory.createEmptyBorder(5,5,5,5)));
        this.setPreferredSize(new Dimension(BORDER/2+ELEVATOR_WIDTH*2*elevators.length+BORDER*elevators.length+BUTTON_WIDTH+BORDER, ELEVATOR_HEIGHT));
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        for (int i = 0; i < doors.length; i++){
            doors[i].draw(g, BORDER+ELEVATOR_WIDTH*2*i+BORDER*i, BORDER);
            g.setColor(Color.black);
            g.drawString("Elevator " + elevatorIdx.get(i), BORDER+ELEVATOR_WIDTH*2*i+BORDER*i, BORDER+ELEVATOR_HEIGHT+15);
            g.drawString(String.valueOf(elevators[elevatorIdx.get(i)].getPosition()), BORDER+ELEVATOR_WIDTH*2*i+BORDER*i+ELEVATOR_WIDTH, 25);
        }
        
    }
        
    protected void updateFloors(short fIdx) throws IOException{
        if (this.floorIdx == fIdx){
            for (int i = 0; i < doors.length; i++){
                if (elevators[i].goesToFloor(floorIdx)){
                    if (elevators[i].getPosition() == floorIdx){
                        doors[i].setOpening(elevators[i].getDoor());
                    } else {
                        doors[i].setOpening(0);
                    }
                }
            }
        } else {
            this.floorIdx = fIdx;
            this.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(floors[floorIdx].getName()), 
                    BorderFactory.createEmptyBorder(5,5,5,5)));
            ArrayList<Elevator> e = new ArrayList<Elevator>();
            elevatorIdx = new ArrayList<Short>();

            for (int i = 0; i < elevators.length; i++) {
                if (elevators[i].goesToFloor(floorIdx)){
                    e.add(elevators[i]);
                    elevatorIdx.add(elevators[i].getIndex());
                }
            }
            if (doors.length != e.size()){
                this.setPreferredSize(new Dimension(BORDER/2+ELEVATOR_WIDTH*2*e.size()+BORDER*e.size()+BUTTON_WIDTH+BORDER, ELEVATOR_HEIGHT));
                doors = new Door[e.size()];
                for (int i = 0; i < e.size(); i++){
                    doors[i] = new Door(ELEVATOR_WIDTH, ELEVATOR_HEIGHT);
                }
            }
        }
        repaint();
    }

}
